package com.nuist.springmvcdemo.ds;

import java.util.Map;
import java.util.TreeMap;

public class Map_test {
    public static void main(String[] args) {
        Map<String, String> m = new TreeMap<>();

        // 使用put 插入key-value的键值对
        m.put("林冲", "豹子头");
        m.put("鲁智深", "花和尚");

        String str = m.put("李逵", "黑旋风");
        System.out.println(str);
        System.out.println(m.size());
        System.out.println(m);

        // get
        System.out.println(m.get("宋江"));
        System.out.println(m.get("李逵"));

        // getOrDefault
        //GetOrDefault(): 如果key存在，返回与key所对应的value，如果key不存在，返回一个默认值
        System.out.println(m.getOrDefault("李逵", "铁牛"));
        System.out.println(m.getOrDefault("史进", "九纹龙"));
        System.out.println(m.size());

        // containKey
        System.out.println(m.containsKey("李逵"));
        System.out.println(m.containsKey("林冲"));
        System.out.println(m.containsKey("史进"));

        // containsValue
        System.out.println(m.containsValue("豹子头"));
        System.out.println(m.containsValue("九纹龙"));


        // keySet  key
        for (String s : m.keySet()) {
            System.out.println(s + " ");
        }

        // values
        for (String s : m.values()) {
            System.out.println(s + " ");
        }

        //entrySet
        for (Map.Entry<String,String> entry : m.entrySet()) {
            System.out.println(entry.getKey() + "的外号是" + entry.getValue());
        }
    }
}
